<?php


namespace App\Http\Controllers;

use PhpOffice\PhpWord\Element\ListItemRun;
use PhpOffice\PhpWord\Element\Section;
use PhpOffice\PhpWord\Element\Text;
use PhpOffice\PhpWord\Element\TextBreak;
use PhpOffice\PhpWord\Element\TextRun;
use PhpOffice\PhpWord\Element\Title;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Shared\ZipArchive;
use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\Shared\Converter;
use PhpOffice\PhpWord\Style\TablePosition;

class WordController extends Controller
{
    public $currentPage = 0;
    private $args = null; // 文本段样式

    public function index()
    {
        $file1 = public_path('word/c.docx');
        $old = IOFactory::load($file1);
        $sections = $old->getSections();
        $phpWord = new PhpWord();
        foreach ($sections as $section) {
            if ($section instanceof Section) {
                $sec = $phpWord->addSection($section->getStyle());
                $elements = $section->getElements();
                foreach ($elements as $key=>$element) {
                    if ($element instanceof TextRun) {
                        if($element instanceof ListItemRun){
                            $secRun = $sec->addListItemRun($element->getDepth(),$element->getStyle(),$element->getParagraphStyle());
                        }else{
                            $secRun = $sec->addTextRun($element->getParagraphStyle());
                        }
                        foreach ($element->getElements() as $item) {
                            if ($item instanceof Text) {
                                $secRun->addText($item->getText(), $item->getFontStyle(), $item->getParagraphStyle());
                            }
                        }
                    } elseif ($element instanceof TextBreak) {
                        $sec->addTextBreak(1, $element->getFontStyle(), $element->getParagraphStyle());
                    } else {
                        dump($element);
                    }
                }
            }
        }
        $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
        $objWriter->save(public_path('word/cc.docx'));
    }
}
